#include "target.h"


#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>

using namespace std;

#include "vect2D/Vect2D.h"

Target::Target(const Vect2D& a_position, const Vect2D& a_speed, SDL_Surface* a_sprite) : Object()
{
	position = a_position;
	speed = a_speed;
	sprite = a_sprite;
	border = 15;
	health = 10;
	src_rect.x = 0;
	src_rect.y = 0;
	src_rect.w = 30;
	src_rect.h = 30;
}

void Target::Frame_calculate()
{
	//if(speed.Get_lenght() < 0.5)
		//remove = true;
	return;
}

void Target::Draw(SDL_Surface* screen, const Vect2D& offset)
{
	Vect2D rel_position = position - offset;

	SDL_Rect dest_rect;
	dest_rect.x = rel_position.Get_x() - src_rect.w/2;
	dest_rect.y = rel_position.Get_y() - src_rect.h/2;

	SDL_BlitSurface(sprite, &src_rect, screen, &dest_rect);

	// draw health bar
	SDL_Rect health_bar_rect;

	health_bar_rect.x = rel_position.Get_x() - health/2;
	health_bar_rect.w = health;

	health_bar_rect.y = rel_position.Get_y() - border - 10;
	health_bar_rect.h = 5;

	SDL_FillRect(screen, &health_bar_rect, SDL_MapRGB(screen->format, 0, 200, 0));
}

/********************************
void Target_generate(...)
A static method that generates new targets in
world with probability. The targets
are drawn with the sprite surface.
********************************/
void Target::Target_generate(World* world, double probability, SDL_Surface* sprite)
{
	// random create seed
	double create = double(rand()%1000)/1000;

	Target* temp;

	if(create <= probability)
	{
		// randomly generated attributes
		//double speed = double(rand()%1000)/1000;
		//double angle = double(rand()%1000)/1000;
		double x_pos = double(rand()%640);
		double y_pos = double(rand()%480);

		// create a new Target object, add it to the world instance
		Vect2D speed_vect = Vect2D(0,0);
		//speed_vect = speed*10;
		//speed_vect.Set_angle(M_PI_2*angle);
		temp = new Target(Vect2D(x_pos,y_pos), speed_vect, sprite);
		world->Add_object(temp);
	}


}

int Target::Hit()
{
	return 0;
}
